package com.example.smartcitydemo.ui.person

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.ReplyBean
import com.example.smartcitydemo.bean.ReplyInfo
import com.example.smartcitydemo.bean.ReplyMessage
import com.example.smartcitydemo.databinding.FragmentReplysubmitBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.viewModel.person.PersonViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.person
 * @NAME: Caleb.F
 * @DATE: 2021/11/12 14:23
 * @Email: 1919796283@qq.com
 */
class ReplySubmitFragment :
    BaseFragment<FragmentReplysubmitBinding>(R.layout.fragment_replysubmit) {
    private val personViewModel: PersonViewModel by viewModels()
    private val replyMessageAdapter: ReplyMessageAdapter by lazy {
        ReplyMessageAdapter().apply {
            init()
            setDiffCallback(ReplyBean.diffCallBack)
        }
    }

    override fun initData() {
        personViewModel.getReplyList()
    }

    override fun initView(bindView: FragmentReplysubmitBinding, savedInstanceState: Bundle?) {
        bindView.run {
            toolbarReply.tvContent.text = "意见反馈"
            replyTypeBar.typeBarText.text = "意见反馈列表"
            recyclerReply.adapter = replyMessageAdapter
            btnSubmit.setOnClickListener {
                val toString = textInputLayout3.editText?.text?.trim().toString()
                if (toString.isEmpty()) {
                    ToastUtils.showShort("请输入意见反馈内容")
                } else {
                    KeyboardUtils.hideSoftInput(it)
                    personViewModel.submitReplyMessage(ReplyMessage(toString, toString))
                }
            }
        }
    }

    override fun addObservers(bindView: FragmentReplysubmitBinding) {
        personViewModel.replyResult.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                ToastUtils.showShort("提交成功")
                personViewModel.getReplyList()
            }
        }

        personViewModel.replyList.observe(viewLifecycleOwner) {
            when(it){
                is Resource.Success ->
                    replyMessageAdapter.setDiffNewData(it.data!!.toMutableList())
                is Resource.Failure->
                    replyMessageAdapter.setEmptyView(R.layout.error_layout)
            }
        }
    }
}